package com.chatsocket.util;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArraySet;

import com.chatsocket.model.Chat;
import com.chatsocket.model.ChatContent;
import com.chatsocket.model.ChatUserRela;
import com.chatsocket.model.ChatVO;
import com.chatsocket.socketServlet.WebSocketServlet;

public class Constant {
	public static List<User> users=new ArrayList<User>();
	public static List<ChatUserRela> chatUserRelas=new ArrayList<ChatUserRela>();
	public static List<ChatContent> chatContent=new ArrayList<ChatContent>();
	public static List<Chat> chats=new ArrayList<Chat>();
	static{
		for(int i=0;i<6;i++){
			User user=new User();
			long id = 1001+i;
			user.setId(id);
			System.out.println("id:"+id);
			user.setUserName("小黑"+(i+1)+"号");
			user.setUserCode("test"+(i+1));
			user.setUserType(0);
			users.add(user);
		}
		for(int i=0;i<users.size();i++){
			User user1=users.get(i);
			for(int j=i+1;j<users.size();j++){
				User user2=users.get(j);
				Chat chat=new Chat();
				chat.setId(Long.parseLong(user1.getId()+""+user2.getId()));
				chat.setChatCode("C"+chat.getId());
				chats.add(chat);
				ChatUserRela chatUserRela1=new ChatUserRela();
				chatUserRela1.setChatId(chat.getId());
				chatUserRela1.setUserId(user1.getId());
				chatUserRela1.setUserType(user1.getUserType().longValue());
				ChatUserRela chatUserRela2=new ChatUserRela();
				chatUserRela2.setChatId(chat.getId());
				chatUserRela2.setUserId(user2.getId());
				chatUserRela2.setUserType(user2.getUserType().longValue());
				chatUserRelas.add(chatUserRela1);
				chatUserRelas.add(chatUserRela2);
			}
		}
	}
	public static User getUser(String userCode){
		User user=null;
		for(User u:users){
			if(u.getUserCode().equals(userCode)){
				return u;
			}
		}
		return user;
	}
	public static User getUser(Long userId){
		User user=null;
		for(User u:users){
			if(userId.longValue()==u.getId().longValue()){
				return u;
			}
		}
		return user;
	}
	public static List<ChatVO> getChatVOsByUserId(Long userId){
		List<ChatVO> chatVOs=new ArrayList<ChatVO>();
		List<Chat> userChats = getChatsByUserId(userId);
		for(Chat userChat:userChats){
			ChatVO chatVO=new ChatVO();
			chatVO.setChatCode(userChat.getChatCode());
			chatVO.setId(userChat.getId());
			List<ChatUserRela> userRelas = getChatUserRelasByChatId(userChat.getId());
			for(ChatUserRela userRela:userRelas){
				User user = getUser(userRela.getUserId());
				chatVO.getUsers().add(user);
				if(userRela.getUserId().longValue()!=userId.longValue()){
					chatVO.setTargUser(user);
				}
			}
			chatVOs.add(chatVO);
		}
		return chatVOs;
	}
	public static Chat getChatByChatId(Long chatId){
		for(Chat chat:chats){
			if(chatId.longValue()==chat.getId().longValue()){
				return chat;
			}
		}
		return null;
	}
	public static List<Chat> getChatsByUserId(Long userId){
		List<Chat> userChats=new ArrayList<Chat>();
		for(ChatUserRela rela:chatUserRelas){
			if(userId.longValue()==rela.getUserId().longValue()){
				Chat chat = getChatByChatId(rela.getChatId());
				userChats.add(chat);
			}
		}
		return userChats;
	}
	public static List<ChatUserRela> getChatUserRelasByChatId(Long chatId){
		List<ChatUserRela> relas=new ArrayList<ChatUserRela>();
		for(ChatUserRela rela:chatUserRelas){
			if(rela.getChatId().longValue()==chatId.longValue()){
				relas.add(rela);
			}
		}
		return relas;
	}
	/**消息缓存*/
	public final static Map<Long,List<SendMsgBody>> msgCacheMap=new HashMap<Long,List<SendMsgBody>>();
	/**用户webSocket缓存*/
    public final static Map<String,WebSocketServlet> webSocketMap=new HashMap<String,WebSocketServlet>();
    /**用户webSocket Set缓存*/
    public final static Map<String,CopyOnWriteArraySet<WebSocketServlet>> webSocketSetMap=new HashMap<String,CopyOnWriteArraySet<WebSocketServlet>>();
    public final static Map<String,Map<Long,List<ChatContent>>> chatContentCacheMap=new HashMap<String,Map<Long,List<ChatContent>>>();
    
    
    /**=============================生产版==========================*/
    /**
     * 消息缓存记录
     * 首键  用户标记符,首值  用户标识符对应的记录
     * 
     */
    public final static Map<String,Message> MSG_CACHE_MAP=new ConcurrentHashMap<String,Message>();
    /**
     * 登陆时建立webSocket时 添加  断开连接时删除
     * 键 用户标识符
     * 值 当前webSocketServlet
     */
    public final static Map<String,CopyOnWriteArraySet<WebSocketServlet>> CURRENT_ONLINE_WEBSOCKET=new ConcurrentHashMap<String,CopyOnWriteArraySet<WebSocketServlet>>();
    /**
     * 标记用户标记符所对应的webSocket(用户http与webSocket对应 需要在建立webSocket时建立好关联)
     * 键 用户标识符
     * 值 在线webSocket 对应的httpServletSessionId
     */
//    public final static Map<String,List<String>> USER_ONLINE_SESSION=new ConcurrentHashMap<String,List<String>>();
    /**
     * 用户发送的消息缓存   用户目标用户接受
     * 键 目标用户标识符
     * 值 消息List
     */
    public final static Map<String,List<Message>> USER_DESTINATION_MSG=new ConcurrentHashMap<String,List<Message>>();
    /**
     * 用户发送的消息缓存  用于当前用户非当前session接收
     * 键 当前用户标识符
     * 值 消息List
     */
    public final static Map<String,List<Message>> USER_ORIGIN_MSG=new ConcurrentHashMap<String,List<Message>>();
    
}
